package cn.edu.nju.ws.sview.wyge.servlets;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import cn.edu.nju.ws.sview.wyge.relations.adataset.CustomDataset;
import cn.edu.nju.ws.sview.wyge.relations.adataset.relations.Relation;
import cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetItem;

/**
 * 给定relation_id以及dataset得到relation的答案
 */
public class ExpandAdd extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ExpandAdd() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String json = null;
		response.setCharacterEncoding("utf-8");
		if (request.getParameter("data") != null) {
			json = request.getParameter("data");
			String str = URLDecoder.decode(json, "UTF-8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			int id = Integer.parseInt((String) regtJSON.get("id"));
			String ds_type = (String) regtJSON.get("ds_type");
			String idStr = (String) regtJSON.get("idStr");
			int ds_id = Integer.parseInt((String) regtJSON.get("ds_id"));
			String s = (String) regtJSON.get("s");
			int p = Integer.parseInt((String) regtJSON.get("p"));
			String o = (String) regtJSON.get("o");

			ArrayList<Integer> docs = new ArrayList<Integer>();
			if (ds_type.equals(DatasetItem.URI_GENRE)) {
				docs.add(ds_id);
			} else if (ds_type.equals(DatasetItem.CUSTOM_GENRE)) {
				docs.addAll(CustomDataset.getURLIDsByCustomDatasetId(ds_id));
			}else if(ds_type.contentEquals(DatasetItem.TEMP_GENRE)){
				String[] strList = idStr.split("_");
				for(String did :strList){
					if(did.trim().length()>0){
						docs.add(Integer.parseInt(did));
					}
				}
			}

			Relation rel = (Relation) request.getSession().getAttribute(
					"relation" + id);

			rel.addTriple(s, p, o);
			
//			String tsttpl = ((Relation) request.getSession().getAttribute("relation" + id)).toString();
//			System.out.println("sessionTriples:"+rel.getPattern().toString()+"\n|tst:"+tsttpl);
			JSONObject result = new JSONObject();
			result.put("result", "OK");
			response.getWriter().print(result);
		}
	}
}
